//
//  StotyDetainsViewController.m
//  B
//
//  Created by Znt on 15/11/6.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "StotyDetainsViewController.h"
#import "Model2.h"
#import "StoryTableViewCell.h"
#import "Model3.h"
@interface StotyDetainsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)NSMutableArray *allDataArray;
@property (nonatomic ,strong)UIView *aView;
@property (nonatomic ,strong)UILabel *textLabel;

@end

@implementation StotyDetainsViewController

- (void) viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    for (UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[BaseView class]]) {
            view.hidden = YES;
        }
    }
}

- (void) viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    for (UIView *view in self.tabBarController.view.subviews) {
        if ([view isKindOfClass:[BaseView class]]) {
            view.hidden = NO;
        }
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[self navigationItem]setTitle:@"精选故事"];
    [self setAllDataArray:[NSMutableArray array]];
    
    [self setTableView: [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 *XWIDTH , 667 *XHIGHT - 64) style:UITableViewStylePlain]];
    [[self tableView]setDelegate:self];
    [[self tableView]setDataSource:self];
    [[self view]addSubview:[self tableView]];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self creatHeardView];
    [self getData:[[self model2] spot_id]];
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
}

-(void)leftButtonAction
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}


- (void)creatHeardView{

    [self setAView:[[UIView alloc]initWithFrame:CGRectMake(0, 0, 375 *XWIDTH, 320 *XHIGHT)]];
    [[self tableView]setTableHeaderView:[self aView]];
    
    UIImageView * imaView = [[UIImageView alloc]initWithFrame:CGRectMake(25 *XWIDTH, 10 *XHIGHT, 50 *XWIDTH, 50 *XHIGHT)];
    [[self aView]addSubview:imaView];
    imaView.layer.masksToBounds = YES;
    imaView.layer.cornerRadius = 25;
    [imaView sd_setImageWithURL:[NSURL URLWithString:_model2.avatar_m] placeholderImage:[UIImage imageNamed:@""]];
    UILabel * alabel = [[UILabel alloc]initWithFrame:CGRectMake(80 *XWIDTH, 20 *XHIGHT, 150 *XWIDTH, 30 *XHIGHT)];
    [[self aView]addSubview:alabel];
    alabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17*XHIGHT];
    alabel.text = [NSString stringWithFormat:@"by%@",_model2.autorName];
    
    UIView * bview = [[UIView alloc]initWithFrame:CGRectMake(35 *XWIDTH, 70 *XHIGHT, 305* XWIDTH, 1*XHIGHT)];
    [[self aView]addSubview:bview];
    [bview setBackgroundColor:[UIColor lightGrayColor]];
    
    
    [self setTextLabel:[[UILabel alloc] initWithFrame:CGRectMake(25  *XWIDTH, 80 *XHIGHT, 325 *XWIDTH, 200 *XHIGHT)]];
//    [[self textLabel]setBackgroundColor:[UIColor orangeColor]];
    [[self textLabel] setNumberOfLines:0];
    [[self aView]addSubview:[self textLabel]];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [[self allDataArray] count];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    Model3 *model = [self allDataArray][[indexPath row]];
    CGFloat height =  [AppTools heightForCell:model.text andFont:13];
    
    return (height + 420)*XHIGHT;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    StoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qCell"];
    if ( !cell ) {
        cell = [[StoryTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"qCell"];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    Model3 *model  = [self allDataArray][[indexPath row]];
    cell.model3 = model;
    
    return cell;
}


- (void)getData:(NSString *)modelID {

    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot?spot_id=%@",modelID];
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        
        NSDictionary *dic = responseObject;
        NSDictionary *dic1 = [dic objectForKey:@"data"];
        NSDictionary *dic2 = [dic1 objectForKey:@"spot"];
        [[self textLabel]setText:[dic2 objectForKey:@"text"]];
        CGFloat height = [AppTools heightForCell:self.textLabel.text andFont:17];
        CGRect tempRect = self.textLabel.frame;
        tempRect.size.height = height;
        self.textLabel.frame = tempRect;
        
        CGRect temp = [[self aView]frame];
        temp.size.height = (height + 140)*XHIGHT;
        [[self aView]setFrame:temp];
        
        NSArray *array = [dic2 objectForKey:@"detail_list"];
        for (NSDictionary *dictionary in array) {
            Model3 *model3 = [[Model3 alloc]init];
            [model3 setValuesForKeysWithDictionary:dictionary];
            [[self allDataArray] addObject:model3];
        }
        
        [self.tableView reloadData];
        NSLog(@"成功")
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
        NSLog(@"失败")
    }];
    

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
